package com.tjian.fafa.user.module.courier.adapter;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.tjian.fafa.user.R;
import com.tjian.fafa.user.module.bean.BaseBean;
import com.tjian.fafa.user.module.callback.ResultCallback;
import com.tjian.fafa.user.module.courier.activity.UserInfoActivity;
import com.tjian.fafa.user.module.courier.bean.Comment;
import com.tjian.fafa.user.module.courier.bean.MoodInfoBean;
import com.tjian.fafa.user.module.courier.constants.HttpConstants;
import com.tjian.fafa.user.module.courier.db.SharedPreferenceDb;
import com.tjian.fafa.user.module.courier.dialog.CustomDialogUpd;
import com.tjian.fafa.user.module.courier.loadimage.AsynImageLoader;
import com.tjian.fafa.user.module.courier.util.HttpUtil;

import net.tsz.afinal.FinalBitmap;

//import org.apache.http.NameValuePair;
//import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

//import net.tsz.afinal.FinalHttp;

@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class ListViewAdapter extends BaseAdapter
{

    private LayoutInflater layoutInflater;
    private Context context;
    private List<MoodInfoBean> allStr;
    AsynImageLoader asynImageLoader = new AsynImageLoader();
    List<String> allString;
    // 暂且用bitmap加载图片
    private FinalBitmap finalBitmap;
    private List<Comment> comList = null;
    private MainListViewAdapter pinglunAdapter = null;

    public ListViewAdapter(Context context, List<MoodInfoBean> allStr)
    {
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.allStr = allStr;
        finalBitmap = FinalBitmap.create(context);
        finalBitmap.configLoadingImage(R.drawable.ic_launcher);
        finalBitmap.configBitmapLoadThreadSize(5);
    }

    @Override
    public int getCount()
    {
        return allStr.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return allStr.get(arg0);
    }

    @Override
    public long getItemId(int arg0)
    {
        return arg0;
    }

    @Override
    public View getView(int arg0, View contentView, ViewGroup arg2)
    {
        RelativeLayout view = null;
        ViewHolder viewHolder;
        if (view == null)
        {
            view = (RelativeLayout) layoutInflater.inflate(R.layout.main_listview_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivUserLogo = (ImageView) view.findViewById(R.id.iv_leftlogo);
            viewHolder.tvUserName = (TextView) view.findViewById(R.id.userName);
            viewHolder.ankuaiNumberTextView = (TextView) view.findViewById(R.id.ankuaiNumberTextViewId);
            viewHolder.accumulativeDeliveryTimesTextView = (TextView) view.findViewById(R.id.accumulativeDeliveryTimesTextViewId);
            viewHolder.averageDeliveryTimeTextView = (TextView) view.findViewById(R.id.averageDeliveryTimeTextViewId);
            viewHolder.applauseRateTextView = (TextView) view.findViewById(R.id.applauseRateTextViewId);
            view.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) view.getTag();
        final MoodInfoBean bean = allStr.get(arg0);
        viewHolder.tvUserName.setText(bean.getUserName());
        viewHolder.ankuaiNumberTextView.setText(bean.getAnkuaiNumber());
        viewHolder.accumulativeDeliveryTimesTextView.setText(bean.getAccumulativeDeliveryTimes());
        viewHolder.averageDeliveryTimeTextView.setText(bean.getAverageDeliveryTime());
        viewHolder.applauseRateTextView.setText(bean.getApplauseRate());

        String image_0 = bean.getImgone();
        String image_1 = bean.getImgtwo();
        String image_2 = bean.getImgthree();
        String image_3 = bean.getImgfour();
        String image_4 = bean.getImgfive();
        String image_5 = bean.getImgsix();
        allString = new ArrayList<String>();
        finalBitmap.display(viewHolder.ivUserLogo, HttpConstants.HTTP_REQUEST
                + bean.getAvatarImagePath());
        /**
         * 对gridview 图片显示做的操作
         */
        if (image_0 != null)
        {
            allString.add(image_0);
        }
        if (image_1 != null)
        {
            allString.add(image_1);
        }
        if (image_2 != null)
        {
            allString.add(image_2);
        }
        if (image_3 != null)
        {
            allString.add(image_3);
        }
        if (image_4 != null)
        {
            allString.add(image_4);
        }
        if (image_5 != null)
        {
            allString.add(image_5);
        }

        if (allString.size() > 0)
        {
            MainGridViewAdapter mainGridViewAdapter = new MainGridViewAdapter(
                    context, allString);
            //说明有两行
            if (allString.size() > 3)
            {
            }
        }

        /**
         * 对用户评论做的操作
         */
        if (null != bean.getAllComment() && bean.getAllComment().size() != 0)
        {
            comList = bean.getAllComment();
            pinglunAdapter = new MainListViewAdapter(context, comList);
        }

        /**
         * 获取用户资料
         */
        viewHolder.ivUserLogo.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {

                Toast.makeText(context, bean.getUserid(), Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.setClass(context, UserInfoActivity.class);
                intent.putExtra("userid", bean.getUserid());
                intent.putExtra("username", bean.getUserName());
                context.startActivity(intent);
            }
        });

        return view;
    }

    public class ViewHolder
    {
        private ImageView ivUserLogo;
        private TextView tvUserName;
        private TextView ankuaiNumberTextView;
        private TextView accumulativeDeliveryTimesTextView;
        private TextView averageDeliveryTimeTextView;
        private TextView applauseRateTextView;
    }


    protected void showUpdataDialog(final String diaryId, final String UserName, final List<Comment> comList)
    {

        final CustomDialogUpd.Builder builder = new CustomDialogUpd.Builder(context);
        builder.setTitle("发表评论");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int arg1)
            {

                String content = builder.getEditText().getText().toString().trim();
                pinglun(diaryId, content, UserName, comList);
                dialog.dismiss();
            }
        });
        // 当点取消按钮时进行登录
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {

                dialog.dismiss();
            }
        });

        builder.create().show();
    }


    public void pinglun(final String diaryId, final String content, final String UserName, final List<Comment> coList)
    {
        if (content.isEmpty())
        {
            Toast.makeText(context, "请填入评论内容", Toast.LENGTH_LONG).show();
            return;
        }
        List<NameValuePair> allP = new ArrayList<NameValuePair>();
        allP.add(new BasicNameValuePair("diaryId", diaryId));
        allP.add(new BasicNameValuePair("cUserId", new SharedPreferenceDb(context).getUserId()));
        allP.add(new BasicNameValuePair("content", content));

        HttpUtil.doPost(HttpConstants.HTTP_PINGLUN, allP, new ResultCallback()
        {

            @Override
            public void getReslt(String result)
            {

                if (!result.isEmpty() && !"1".equals(result))
                {
                    BaseBean b = JSON.parseObject(result, BaseBean.class);
                    if ("0".equals(b.getRespcode()))
                    {
                        Toast.makeText(context, String.valueOf(result), Toast.LENGTH_LONG).show();
                        if (coList == null || coList.size() == 0)
                        {
                            comList = new ArrayList<Comment>();
                            comList.add(new Comment("", diaryId, "", content, "", UserName));
                            pinglunAdapter = new MainListViewAdapter(context, comList);
                        }
                        else
                        {
                            comList.add(new Comment("", diaryId, "", content, "", UserName));
                            pinglunAdapter.notifyDataSetChanged();
                        }

                    }
                    else
                    {
                        Toast.makeText(context, "用户名或密码错误", Toast.LENGTH_LONG).show();
                    }
                }
                else
                {
                    Toast.makeText(context, "服务器响应失败", Toast.LENGTH_LONG).show();
                }
            }
        });


    }

}
